package com.ewhale.feitengguest.presenter.user.auth;

import com.ewhale.feitengguest.api.ApiHelper;
import com.ewhale.feitengguest.dto.ArticleDetailDto;
import com.ewhale.feitengguest.dto.LoginInfoDto;
import com.ewhale.feitengguest.view.auth.LoginView;
import com.ewhale.feitengguest.view.auth.RegisterView;
import com.simga.library.base.IPresenter;
import com.simga.library.http.APIException;
import com.simga.library.utils.CheckUtil;
import com.simga.library.utils.LogUtil;

import java.util.HashMap;
import java.util.Map;

public class LoginPresenter extends IPresenter {

    private String userRegister = "app/common/registerByPhone.json";
    private String shopRegister = "app/common/shopRegister.json";

    private String userLogin = "app/common/login.json";
    private String shopLogin = "app/common/shopLogin.json";

    private String userThridLogin = "app/common/loginByThird.json";
    private String shopThridLogin = "app/common/shopLoginByThird.json";


    public void getCode(String sessionId, String registerPhone) {
        mView.showProLoading();
        request(1, ApiHelper.AUTH_API.getCode(sessionId, registerPhone, "1"), null);
    }

    public void register(int loginType, String registerPhone, String code, String registerPwd, String inviteCode) {
        mView.showProLoading();
        Map<String, Object> params = new HashMap<>();
        if (loginType == 0) {
            params.put("phone", registerPhone);
            params.put("phoneCode", code);
            params.put("password", registerPwd);
            params.put("repeatPassword", registerPwd);
            if (!CheckUtil.isNull(inviteCode)) {
                params.put("recommendCode", inviteCode);
            }
            request(2, ApiHelper.AUTH_API.register(userRegister, params), 1);
        } else {
            params.put("userName", registerPhone);
            params.put("phoneCode", code);
            params.put("password", registerPwd);
            params.put("repeatPassword", registerPwd);
            params.put("userType", inviteCode.equals("个人") ? 1 : 2);
            request(2, ApiHelper.AUTH_API.register(shopRegister, params), 1);
        }
    }

    public void login(int loginType, String phone, String password) {
        mView.showProLoading();
        if (loginType == 0) {
            request(3, ApiHelper.AUTH_API.login(userLogin, phone, password), null);
        } else {
            request(3, ApiHelper.AUTH_API.login(shopLogin, phone, password), null);
        }
    }

    public void loadArticleDetails(int id) {
        mView.showProLoading();
        request(4, ApiHelper.USER_API.getArticleDetails(id), id);
    }

    public void loginThrid(int loginType, String openId, String name, String iamgeUrl, int type) {
        mView.showProLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("id", openId);
        params.put("nickName", name);
        params.put("headPhoto", iamgeUrl);
        params.put("type", type);
        params.put("loginType", loginType);
        if (loginType == 0) {
            request(5, ApiHelper.AUTH_API.loginByThird(userThridLogin, params), params);
        } else {
            request(5, ApiHelper.AUTH_API.loginByThird(shopThridLogin, params), params);
        }
    }


    @Override
    public <T> void onNext(int postCode, T result, Object data) {
        super.onNext(postCode, result, data);
        switch (postCode) {
            case 1:
                ((RegisterView) mView).getCodeSuccess();
                break;
            case 2:
                ((RegisterView) mView).registerOrLoginSuccess((LoginInfoDto) result);
                break;
            case 3:
                ((LoginView) mView).registerOrLoginSuccess((LoginInfoDto) result);
                break;
            case 4:
                if (mView instanceof LoginView) {
                    ((LoginView) mView).showDetail((ArticleDetailDto) result, (int) data);
                } else if (mView instanceof RegisterView) {
                    ((RegisterView) mView).showDetail((ArticleDetailDto) result, (int) data);
                }
                break;
            case 5:
                ((LoginView) mView).loginThird((LoginInfoDto) result, (Map<String, Object>) data);
                break;
        }
    }

    @Override
    public void otherException(int postCode, Object data, APIException e) {
        super.otherException(postCode, data, e);
        mView.showErrorMessage(postCode, e.getCode(), e.getDisplayMessage());
    }
}
